<template>
	<view>
		<bg-video></bg-video>
		<cu-custom bgColor="bg-main" :isBack="true">
			<block class="text-white" slot="content">设置-店长</block>
		</cu-custom>
		<view>
			<view class="solid-bottom">
				<view class="cu-form-group flex justify-between">
					<button class=" bg-white  headimg margin-0" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<view class="cu-avatar lg round" :style="'background-image:url('+adz.cover+');'"></view>
					</button>
					<view class="flex justify-end align-center text-lg">
						<input class="text-right w-75 text-df padding-0" type="nickname" v-model="adz.dname" placeholder="请输入昵称" />
						<text class="cuIcon-right text-gray "></text>
					</view>
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">电话</view>
					<view class="flex justify-end align-center text-lg">
						<button class="phone-button text-df text-gray" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">{{adz.tel ? adz.tel : '请输入手机号'}}</button>
						<text class="cuIcon-right text-gray "></text>
					</view>
					
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">人脸</view>
					<view class="flex justify-end align-center">
						<view v-if="adz && adz.tx" @click="openTx" class="relative cu-avatar lg bg-inherit">
							<view  class="cu-avatar lg round absolute top0 right0"
								:style="'background-image:url('+adz.tx+');'"></view>
							<view
								class="cu-avatar lg round absolute top0 right0 bg-cover"></view>
						</view>
						<text v-else @click="openTx" class="text-df text-gray">未设置</text>
						<text class="cuIcon-right text-gray"></text>
					</view>
				</view>
			</view>
		</view>
		<view class="padding ">
			<button @click="save" class="cu-btn block bg-main lg">保存</button>
			<view class="padding-top">
				<text v-if="pflg" @tap="pflg = !pflg" class="cuIcon-roundcheckfill text-main">同意开店协议</text>
				<text v-else @tap="pflg = !pflg" class="cuIcon-round text-main">同意开店协议</text>
			</view>
		</view>
		<view v-if="pflg" class="bg-white padding-sm">
			<home></home>
		</view>
	</view>
</template>

<script>
	import upload from "@/wx-cos/upload.js";
	import homeVue from './home.vue';
	export default {
		components: {
			"home": homeVue,
		},
		data: function() {
			return {
				txurl: "",
				adz: this.store.state.dz,
				pflg: false,
			};
		},
		onShareAppMessage: function() {
			return {
				title: this.store.state.title,
				path: '/pages/first?id=' + this.store.state.client.clientid
			}
		},
		methods: {
			getPhoneNumber: function(e) {
				let phoneCode = e.detail.code;
				
				if (phoneCode) {
					this.api("/dz/phone").send({
						dzid: this.store.state.dz.dzid,
						phoneCode,
					}).then(data => {
						this.store.commit("submitDz", data.adz);
						this.adz.tel = data.adz.tel;
						this.message.info("电话设置成功");
					});
				}

			},
			onChooseAvatar: function(e) {
				// 上传图片
				// 得到图片云服务地址
				upload.checkUploadFile(
					e.detail.avatarUrl,
					this.adz.clientid,
					"headimg", pro => {}
				).then(data => {
					this.adz.cover = data.url;

				});
			},
			openTx: function() {
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					sizeType: ['original'],
					camera: "back",
					success: res => {
						upload.checkUploadFile(
							res.tempFiles[0].tempFilePath,
							this.adz.clientid,
							"face", progressData => {
								console.log(progressData);
							}).then((data) => {
							this.adz.tx = data.url;
						});
					}
				})
			},
			save: function() {
				
				if (!this.adz.cover) {
					this.message.info("请先设置头像");
					return;
				}
				
				if (!this.adz.dname) {
					this.message.info("请先设置店长名称");
					return;
				}
				
				if (!this.adz.tel) {
					this.message.info("请先设置联系电话");
					return;
				}
				
				if (!this.adz.tx) {
					this.message.info("店长人脸尚未录入");
					return;
				}
				
				if (!this.pflg) {
					this.message.info("请先同意开店协议");
					return;
				}
				
				this.api("/dz/info/put").send({
					adz: this.adz
				}).then(res => {
					this.showFlg = true;
					this.adz = res.adz;
					this.store.commit("submitDz", res.adz);
					this.message.info("设置成功");
					uni.navigateBack();
				});
			}
		}
	}
</script>

<style scoped>
	.headimg {
		padding-left: 0;
		padding-right: 0;
	}

	.headimg::after {
		border: none;
	}
	
	.phone-button {
		border-style: none;
		background-color: rgba(0, 0, 0, 0);
		display: inline;
		padding: 0 0 0 5px;
		margin: 0;
	}
	
	.phone-button::after {
		border-style: none;
		background-color: rgba(0, 0, 0, 0);
	}
</style>